<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * Controller for managing companies.
 * @package backend-companies/conrollers
 * @category Controller
 * 
 * @author Mark Angelo Angulo
 * @copyright (c) 2015
 */
class Company extends Backend_Controller {

    /**
     *  Constructor method.
     */
    function __construct()
    {
        parent::__construct();
        
        $this->load->model('company_model');

        $this->load->helper('cms');
        $this->load->helper('company');
        
        $this->load->library('form_validation');
        $this->form_validation->set_error_delimiters('<div class="clear-fix"></div><small>', '</small>');
        
        init_company();
    }

    /**
     * Default method called when nothing is specified.
     * Displays dashboard.
     * @access public
     */
    public function index()
    {
        view_companies_list();
    }
    
    public function company_datatable()
    {
        $params = $this->uri->uri_to_assoc();
        if( isset($params['status']) )
        {
            $status = $params['status'];
//            $args = set_status($status, $this->db_table);
//            $args = set_status($status, 'ht_companies');
            
            echo $this->company_model
                    ->all_datatable
                    (
                        array(), 
                        $this->alias, 
                        $this->custom_columns, 
                        'companies', 
                        TRUE
                    );
        } else
        {
            view_companies_list();
        }
    }
    
    /*
     * Method called for adding a new record
     * @access public
     */
    public function add()
    {
        if( isset($_POST) && !empty($_POST) )
        {
            if ( valid_add_company() )
            {
                $logo = ( ! empty($_FILES['logo']) && $_FILES['logo']['error'] != 4 ) ? valid_image_upload('logo', $this->logo_path, 'add', 'jpg|gif|png') : TRUE;
                if( $logo )
                {
                    $company            = array();
                    $company['image']   = $logo === TRUE ? NULL : $logo;
                    
                    set_value('name')           ? $company['name']           = clean_text( set_value('name') )               : NULL;
                    set_value('name')           ? $company['url']            = changeCase( set_value('name'), 'dash')        : NULL;
                    set_value('description')    ? $company['description']    = clean_text( set_value('description') )        : NULL;
                    set_value('status')         ? $company['status']         = clean_text( set_value('status') )             : 'active';

                    $this->company_model->insert($company);

                    // add another
                    if( $this->input->post('add-another') )
                    {
                        $this->form_validation->unset_field_data();
                        view_company_form('Add Company', 'add_success');
                    } else
                    {
                        view_companies_list(NULL, 'add_success');
                    }
                    
                } else
                {
                    view_company_form('Add Company', 'add_failed');
                }

            } else
            {
                view_company_form('Add Company', 'add_failed');
            }
            
        } else
        {
            view_company_form('Add Company');
        }
    }
    
    /**
     * Method called for editing a new record.
     * @access public
     */
    public function edit()
    {
        $params = $this->uri->uri_to_assoc();

        if( isset($_POST) && ! empty($_POST) )
        {
            if ( valid_edit_company() )
            {
                $edit_company = $this->company_model->find( set_value('id') )->get();
                $logo = ( ! empty($_FILES['logo']) && $_FILES['logo']['error'] != 4 ) ? valid_image_upload('logo', $this->logo_path, 'edit', 'jpg|gif|png', NULL, NULL, $edit_company['image']) : TRUE;
                if( $logo )
                {
                    $company = array();
                    $logo !== TRUE ? $company['image'] = $logo : '';

                    set_value('name')           ? $company['name']           = clean_text( set_value('name') )               : '';
                    set_value('name')           ? $company['url']            = changeCase( set_value('name'), 'dash')        : NULL;
                    set_value('description')    ? $company['description']    = clean_text( set_value('description') )        : '';
                    set_value('status')         ? $company['status']         = clean_text( set_value('status') )             : '';
                    $company['date_modified']    = date('Y-n-d H:i:s');

                    $this->company_model->update( array('id' => set_value('id')), $company);
                    view_companies_list(NULL, 'edit_success');
                } else
                {
                    $company = $this->company_model->find(set_value('id'))->get();
                    view_company_form('Edit Company', 'edit_failed', $company);
                }
                    
            } else
            {
                $company = $this->company_model->find(set_value('id'))->get();
                view_company_form('Edit Company', 'edit_failed', $company);
            }
        } else
        {
            if( isset($params['id']) && valid_get_id($params['id']) )
            {
                $company = $this->company_model->find($params['id'])->get();
                view_company_form('Edit Company', NULL, $company);
            } else
            {
                $this->show_404();
            }
        }
    }
    
    /**
     * Method called when deleting a record.
     * @access public
     */
    public function delete()
    {
        $params = $this->uri->uri_to_assoc();
        if( isset($params['id']) && valid_get_id($params['id']) )
        {
            // delete or unlink files
            $delete_company = $this->company_model->find( $params['id'] )->get();
            if(isset($delete_company) && ! empty($delete_company['image']))
            {
                unlink(FCPATH . $this->logo_path . $delete_company['image']);
            }
            
            // delete from database
            $deleted = $this->company_model->delete(array('id' => $params['id']));
            if($deleted === 'deleted')
            {
                view_companies_list(NULL, 'delete_success');
            } else
            {
                view_companies_list(NULL, 'archived');
            }
        } else
        {
            $this->show_404();
        }
    }
    
    /*
     * Preview information about a record
     * @access public
     */
    public function preview()
    {
        $params = $this->uri->uri_to_assoc();
        if( isset($params['id']) && valid_get_id($params['id']) )
        {
            $filters = array();
            $filters['company'] = $params['id'];
            $this->session->set_userdata('filters', $filters);
            
            view_company_preview($params['id']);
        } else
        {
            $this->show_404();
        }
    }
    
    /**
     * Method called when deleting multiple companies.
     * @access public
     */
    public function delete_multiple()
    {        
        if(valid_ids())
        {
            $items = $this->input->post('items');
            $archived = $this->company_model->archive($items);
            
            if(count($archived) > 0)
            {
                view_list(NULL, 'archived');
            } else
            {
                view_list(NULL, 'delete_success');
            }
        } else
        {
            $this->show_404();
        }
    }
    
}

/* End of file company.php */
/* Location: ./application/companies/backend-companies/controllers/company.php */